package com.atguigu.controller;

import com.atguigu.base.BaseController;
import com.atguigu.base.BaseDao;
import com.atguigu.entity.HouseUser;
import com.atguigu.service.HouseUserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
@RequestMapping("/houseUser")
public class HouseUserController extends BaseController {

    @DubboReference
    private HouseUserService houseUserService;

    //进入房东新增页面
    @GetMapping("/create")
    public String create(@RequestParam("houseId") Long houseId, Model model) {
        model.addAttribute("houseId", houseId);
        return "houseUser/create";
    }

    //保存新增的房东到数据库
    @PostMapping("/save")
    public String save(HouseUser houseUser) {
        houseUserService.insert(houseUser);
        return "common/successPage";
    }

    //根据id查询房东信息
    @GetMapping("/edit/{id}")
    public String edit(Model model,@PathVariable Long id) {
        HouseUser houseUser = houseUserService.getById(id);
        model.addAttribute("houseUser",houseUser);
        return "houseUser/edit";
    }

    //对用id查询到的房东信息进行修改
    @PostMapping(value="/update")
    public String update(HouseUser houseUser) {
        houseUserService.update(houseUser);
        return "common/successPage";
    }

    //删除房东信息
    @GetMapping("/delete/{houseId}/{id}")
    public String delete(@PathVariable Long houseId, @PathVariable Long id) {
        houseUserService.delete(id);
        return "redirect:/house/" + houseId;
    }

}
